<?php

/**
 * 发布需要爆料的信息（按照模板）
 */
if($uri == "/api/admin/release"){
    $postJson = json_decode(file_get_contents('php://input'), true);
    $template_id = isset($postJson['template_id']) ? intval($postJson['template_id']) : 0;
    $data = isset($postJson['data']) ? $postJson['data'] : "";
    $template = $cnSql->read_only("templates", "id = $template_id");
    if(!$template){
        $outJson->msg = "模板不存在";
        $outJson->code = 400;
        echo json_encode($outJson);
        exit;
    } 
    $cnSql->insert_json("releases", array(
        "template_id" => $template_id,
        "data" => $data,
        "create_time" => date("Y-m-d H:i:s"),
        "status" => 0
    ));

    $outJson->msg = "发布成功";
    $outJson->code = 200;
    echo json_encode($outJson);
    exit;

}


/**
 * 获取统计数据
 */
if($uri == "/api/admin/statistics"){
    $template_id = isset($_GET['template_id']) ? intval($_GET['template_id']) : null;
    
    // 构建查询条件
    $where = "1=1";
    if($template_id) {
        $where .= " AND template_id = $template_id";
    }
    
    // 统计各状态数量
    $stats = $cnSql->custom("SELECT 
        SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) as status_0,
        SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) as status_1,
        SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) as status_2,
        SUM(CASE WHEN status = 3 THEN 1 ELSE 0 END) as status_3,
        SUM(CASE WHEN status = 4 THEN 1 ELSE 0 END) as status_4,
        SUM(CASE WHEN status = 5 THEN 1 ELSE 0 END) as status_5,
        COUNT(*) as total
        FROM releases WHERE $where")[0];
    
    $outJson->data = array(
        "status_0" => intval($stats['status_0']), // 未使用
        "status_1" => intval($stats['status_1']), // 占用中
        "status_2" => intval($stats['status_2']), // 待审核
        "status_3" => intval($stats['status_3']), // 已通过
        "status_4" => intval($stats['status_4']), // 已拒绝
        "status_5" => intval($stats['status_5']), // 异常
        "total" => intval($stats['total'])        // 总数
    );
    $outJson->msg = "获取成功";
    $outJson->code = 200;
    echo json_encode($outJson);
    exit;
}



/**
 * 获取爆料列表
 */
if($uri == "/api/admin/releases"){
    $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
    $size = isset($_GET['size']) ? intval($_GET['size']) : 20;
    $status = isset($_GET['status']) && $_GET['status'] !== '' ? intval($_GET['status']) : null;
    $template_id = isset($_GET['template_id']) ? intval($_GET['template_id']) : null;
    
    $offset = ($page - 1) * $size;
    
    // 构建查询条件
    $where = "1=1";
    if ($template_id) {
        $where .= " AND template_id = $template_id";
    }
    if($status !== null){
        $where .= " AND status = $status";
    }
    
    // 按ID倒序排列，获取指定页的数据
    $sql = $cnSql->read("releases", $page, $size, $where,$total);
    $outJson->data = $sql;
    $outJson->total = intval($total);
    $outJson->code = 200;
    echo json_encode($outJson);
    exit;
}

/**
 * 按id删除爆料
 */
if($uri == "/api/admin/release/delete"){
    $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
    $cnSql->delete("releases", "id = $id");
    $outJson->msg = "删除成功";
    $outJson->code = 200;
    echo json_encode($outJson);
    exit;
}

/**按id重置爆料数据状态 */
if($uri == "/api/admin/releases/batch/reset"){
    $postJson = json_decode(file_get_contents("php://input"));
    $ids = $postJson->ids;
    foreach($ids as $id){
        if($id){
            $cnSql->update_json("releases", array("status" => 0), "id = $id");
        }
    }
    $outJson->msg = "重置成功";
    $outJson->code = 200;
    echo json_encode($outJson);
    exit;
}